import 'package:flutter/material.dart';
import 'package:lib_uikit/lib_uikit.dart';

/// @Title   公共的下指示器的tabbar
///   指示器宽度为文字的60%，指示器计算宽度和位置计算不包括文字后面的括号内容
/// @Author: bryce
/// @CreateDate: 2023年10月11日15:09:30
class GTUnderlineTabBarV3 extends StatelessWidget {
  final List<String> tabTitles;
  final TabController? controller;
  final EdgeInsetsGeometry? padding;

  // 新增tab切换拦截，如果返回true代表拦截，false允许切换
  final Future<bool> Function(TabController mController, int newIndex)? onInterceptSwitch;

  // 新增拦截点击tab时的切换（对于使用controller控制的和手势滚动TabBarView的切换不拦截）
  final Future<bool> Function(TabController mController, int newIndex)? onInterceptTapSwitch;
  final bool? showDivider;
  final CrossAxisAlignment? crossAxisAlignment;
  final List<Widget>? widget;
  //适配标题栏tabBar
  final bool? isBold;

  const GTUnderlineTabBarV3({
    super.key,
    required this.tabTitles,
    this.controller,
    this.padding,
    this.onInterceptSwitch,
    this.onInterceptTapSwitch,
    this.showDivider = false,
    this.crossAxisAlignment = CrossAxisAlignment.start,
    this.isBold = false,
    this.widget
  });

  @override
  Widget build(BuildContext context) {
    bool _showDivider = showDivider ?? false;
    EdgeInsets labPadding = GTEdgeInsetsOnly(right: 12);
    TextStyle labelStyle = gtTextStyleW600(fontSize: 16);
    TextStyle unselectedLabelStyle = gtTextStyleW500(fontSize: 16);
    // 小括号style
    TextStyle bracketsStyle = gtTextStyleW500(fontSize: 14);

    if (isBold == true) {
      labelStyle = gtTextStyleW700(fontSize: 18);
      unselectedLabelStyle = gtTextStyleW500(fontSize: 18);
      labPadding = GTEdgeInsetsOnly(right: 16);
      _showDivider = false;
    }

    return Column(
      crossAxisAlignment: crossAxisAlignment ?? CrossAxisAlignment.center,
      mainAxisSize: MainAxisSize.min,
      children: [
        GTTabBar(
          controller: controller,
          tabs: tabs(bracketsStyle),
          isScrollable: true,
          labelStyle: labelStyle.copyWith(height: 1),
          unselectedLabelStyle: unselectedLabelStyle.copyWith(height: 1),
          labelColor: GTColorsV3.uikitText1V3.color,
          unselectedLabelColor: GTColorsV3.uikitText3V3.color,
          labelPadding: labPadding,
          padding: padding,
          onInterceptSwitch: onInterceptSwitch,
          onInterceptTapSwitch: onInterceptTapSwitch,
          overlayColor: MaterialStateProperty.resolveWith<Color?>(
            (Set<MaterialState> states) => Colors.transparent,
          ),
          indicator: const BoxDecoration(),
        ),
        if (_showDivider) ...[
          Container(
            height: 1,
            color: GTColorsV3.divider2.color,
          ),
        ]
      ],
    );
  }

  List<Widget> tabs(TextStyle bracketsStyle) {
    return tabTitles.map((e) {
      String currentTitle = e.trim();
      // 获取末尾小括号
      String? bracketsStr = TextUtils.getEndBrackets(currentTitle);
      if (TextUtils.isNotEmpty(bracketsStr)) {
        currentTitle = currentTitle.replaceAll(bracketsStr!, '')..trim();
        return GTTab(
            height: 28,
            alignment: isBold == true ? Alignment.center : Alignment.topCenter,
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(currentTitle),
                const SizedBox(width: 2),
                Text(
                  bracketsStr,
                  style: bracketsStyle,
                )
              ],
            ));
      }
      return GTTab(
          height: 28, alignment: isBold == true ? Alignment.center : Alignment.topCenter, child: Text(currentTitle));
    }).toList();
  }
}
